
package com.mego.tzadd.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.mego.tzadd.R;
import com.mego.tzadd.database.dbmodel.Jtdetailinfo;
import com.mego.tzadd.database.manager.JtinfoCenter;
import com.mego.tzadd.utils.HttpRequest;

import zuo.biao.library.base.BaseFragment;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.BottomMenuWindow;
import zuo.biao.library.ui.TopMenuWindow;
import zuo.biao.library.util.JSON;

public class JtInfoFragment1 extends BaseFragment implements View.OnClickListener, AlertDialog.OnDialogButtonClickListener {
    private final String TAG = "JtInfoFragment1";

    //与Activity通信<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    /**
     * 创建一个Fragment实例
     *
     * @return
     */
    public static JtInfoFragment1 createInstance() {
        return new JtInfoFragment1();
    }

    //与Activity通信>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.jtinfofragment1);

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>

        return view;//返回值必须为view
    }

    private ImageView iv_tbts;
    private ImageView iv_chts;
    private ImageView iv_zdxb;

    private EditText edit_zdxbje;
    private TextView tv_zdxbtime0;
    private TextView tv_zdxbtime1;

    @Override
    public void initView() {//必须在onCreateView方法内调用
        iv_tbts = (ImageView) findViewById(R.id.iv_tbts);
        iv_chts = (ImageView) findViewById(R.id.iv_chts);
        iv_zdxb = (ImageView) findViewById(R.id.iv_zdxb);

        edit_zdxbje = (EditText) findViewById(R.id.edit_zdxbje);
        tv_zdxbtime0 = (TextView) findViewById(R.id.tv_zdxbtime0);
        tv_zdxbtime1 = (TextView) findViewById(R.id.tv_zdxbtime1);
    }

    public Jtdetailinfo _jtinfo = null;

    //示例代码>>>>>>>>>
    @Override
    public void initData() {//必须在onCreateView方法内调用
        _jtinfo = JtinfoCenter.getInstance().jtdetailinfo;

        _tbts = _jtinfo.getToubi_switch() == 1 ? true : false;
        _chts = _jtinfo.getChuhuo_switch() == 1 ? true : false;
        _zdxb = _jtinfo.getAuto_switch() == 1 ? true : false;
        updateTbts();
        updateChts();
        updateZdxb();

        _zdxbje = _jtinfo.getZdxb();
        _zdxbtime0 = _jtinfo.getTime() / 60;
        _zdxbtime1 = _jtinfo.getTime() % 60;
        edit_zdxbje.setText(_zdxbje + "");
        tv_zdxbtime0.setText(_zdxbtime0 + "");
        tv_zdxbtime1.setText(_zdxbtime1 + "");

    }
    //Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    //Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {//必须在onCreateView方法内调用
        findViewById(R.id.tv_save).setOnClickListener(this);
        findViewById(R.id.tv_reset).setOnClickListener(this);
        findViewById(R.id.iv_tbts).setOnClickListener(this);
        findViewById(R.id.iv_chts).setOnClickListener(this);
        findViewById(R.id.iv_zdxb).setOnClickListener(this);
//        findViewById(R.id.edit_zdxbje).setOnClickListener(this);

        findViewById(R.id.tv_zdxbtime0).setOnClickListener(this);
        findViewById(R.id.tv_zdxbtime1).setOnClickListener(this);

    }

    int _zdxbje = 0;
    int _zdxbtime0 = 0;//小时
    int _zdxbtime1 = 0;//分钟

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_save:
                onSave();
                break;
            case R.id.tv_reset:
                onReset();
                break;
            case R.id.iv_tbts:
                onTbts();
                break;
            case R.id.iv_chts:
                onChts();
                break;
            case R.id.iv_zdxb:
                onZdxb();
                break;
            case R.id.edit_zdxbje:
//                popZdxbje();
                break;
            case R.id.tv_zdxbtime0:
                popZdxbtime0();
                break;
            case R.id.tv_zdxbtime1:
                popZdxbtime1();
                break;

        }
    }

    boolean _tbts = true;
    boolean _chts = true;
    boolean _zdxb = true;

    private void onTbts() {

        new AlertDialog(getContext(), "", "確認修改?", "確認", "取消", 14, this).show();
    }

    void updateTbts() {
        if (_tbts) {
            iv_tbts.setImageResource(R.mipmap.btn_turn_on);
        } else {
            iv_tbts.setImageResource(R.mipmap.btn_turn_off);
        }
    }

    private void onChts() {

        new AlertDialog(getContext(), "", "確認修改?", "確認", "取消", 15, this).show();
    }

    void updateChts() {
        if (_chts) {
            iv_chts.setImageResource(R.mipmap.btn_turn_on);
        } else {
            iv_chts.setImageResource(R.mipmap.btn_turn_off);
        }
    }

    private void onZdxb() {
        _zdxb = !_zdxb;
        updateZdxb();
    }

    void updateZdxb() {
        if (_zdxb) {
            iv_zdxb.setImageResource(R.mipmap.btn_turn_on);
        } else {
            iv_zdxb.setImageResource(R.mipmap.btn_turn_off);
        }
    }

    boolean _issaveing = false;

    private void onSave() {
        new AlertDialog(getContext(), "", "確認保存?", "確認", "取消", 11, this).show();
    }

    private void onReset() {
        new AlertDialog(getContext(), "", "請確認重新啟動\n [" + _jtinfo.getName() + "] ?", "確認", "取消", 13, this).show();
    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }

        if (requestCode == 11) {
            //上传信息
            doSave();
        } else if (requestCode == 13) {
            //上传信息
            doReset();
        } else if (requestCode == 14) {
            //上传信息
            _tbts = !_tbts;
            updateTbts();
            doSave2();
        } else if (requestCode == 15) {
            //上传信息
            _chts = !_chts;
            updateChts();
            doSave2();
        }
    }

    private void doSave() {
        if (_issaveing) {
            return;
        }

        String strZdxb = edit_zdxbje.getText().toString();
        if (strZdxb == null || strZdxb.isEmpty()) {
            showShortToast("自動消保金額設定錯誤!");
            return;
        }

        int zdxb = Integer.parseInt(strZdxb);
        if (zdxb > 9990) {
            showShortToast("自動消保金額不能大于9990!");
            return;
        }

        _jtinfo.setToubi_switch(_tbts ? 1 : 0);
        _jtinfo.setChuhuo_switch(_chts ? 1 : 0);
        _jtinfo.setAuto_switch(_zdxb ? 1 : 0);
        _jtinfo.setZdxb(zdxb);

        int h = Integer.parseInt(tv_zdxbtime0.getText().toString());
        int min = Integer.parseInt(tv_zdxbtime1.getText().toString());
        _jtinfo.setTime(h * 60 + min);

        _issaveing = true;
        showProgressDialog(R.string.loading);
        try {
            HttpRequest.postjtinfo(_jtinfo, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    if (code == 1) {
                        showShortToast("機台設置成功訊息！");
                    } else {
                        showShortToast("機台無回應訊息！");
                    }
                    dismissProgressDialog();
                    _issaveing = false;
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
            _issaveing = false;
        }
    }

    private void doSave2() {
        if (_issaveing) {
            return;
        }

        _jtinfo.setToubi_switch(_tbts ? 1 : 0);
        _jtinfo.setChuhuo_switch(_chts ? 1 : 0);
        _issaveing = true;
        showProgressDialog(R.string.loading);
        try {
            HttpRequest.postjtinfo2(_jtinfo, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    if (code == 1) {
                        showShortToast("機台設置成功訊息！");
                    } else {
                        showShortToast("機台無回應訊息！");
                    }
                    dismissProgressDialog();
                    _issaveing = false;
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
            _issaveing = false;
        }
    }

    private void doReset() {
        if (_issaveing) {
            return;
        }

        _issaveing = true;
        showProgressDialog(R.string.loading);
        try {
            HttpRequest.jtreset(_jtinfo.getJtid(), 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    if (code == 1) {
                        showShortToast("重啟成功！");
                    } else {
                        showShortToast("重啟失败！");
                    }
                    dismissProgressDialog();
                    _issaveing = false;
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
            _issaveing = false;
        }
    }

    String[] ZDXBJE = new String[20];

    void popZdxbje() {
        for (int i = 0; i < 20; i++) {
            ZDXBJE[i] = (i + 1) * 100 + "";
        }
        toActivity(BottomMenuWindow.createIntent(context, ZDXBJE)
                .putExtra(BottomMenuWindow.INTENT_TITLE, "選擇自動消保金額"), REQUEST_TO_BOTTOM_MENU_0, false);
    }

    String[] ZDXBTIMEO = new String[30];

    private void popZdxbtime0() {
        for (int i = 0; i < 30; i++) {
            ZDXBTIMEO[i] = i + "";
        }
        toActivity(BottomMenuWindow.createIntent(context, ZDXBTIMEO)
                .putExtra(BottomMenuWindow.INTENT_TITLE, "選擇自動消保時間(小時)"), REQUEST_TO_BOTTOM_MENU_1, false);
    }

    String[] ZDXBTIME1 = new String[60];

    private void popZdxbtime1() {
        for (int i = 0; i < 60; i++) {
            ZDXBTIME1[i] = i + "";
        }
        toActivity(BottomMenuWindow.createIntent(context, ZDXBTIME1)
                .putExtra(BottomMenuWindow.INTENT_TITLE, "選擇自動消保時間(分鐘)"), REQUEST_TO_BOTTOM_MENU_2, false);
    }

    private final int REQUEST_TO_BOTTOM_MENU_0 = 31;
    private final int REQUEST_TO_BOTTOM_MENU_1 = 32;
    private final int REQUEST_TO_BOTTOM_MENU_2 = 33;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_TO_BOTTOM_MENU_0:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < ZDXBJE.length) {
                        edit_zdxbje.setText(ZDXBJE[selectedPosition]);
                    }
                }
                break;
            case REQUEST_TO_BOTTOM_MENU_1:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < ZDXBTIMEO.length) {
                        tv_zdxbtime0.setText(ZDXBTIMEO[selectedPosition]);
                    }
                }
                break;
            case REQUEST_TO_BOTTOM_MENU_2:
                if (data != null) {
                    int selectedPosition = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
                    if (selectedPosition >= 0 && selectedPosition < ZDXBTIME1.length) {
                        tv_zdxbtime1.setText(ZDXBTIME1[selectedPosition]);
                    }
                }
                break;
        }
    }

}